package com.heima.service.impl;


import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.*;
import com.heima.service.NodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class NodeServiceImpl implements NodeService {
    @Autowired
    private NodeMapper nodeMapper;

    @Override
    public List<Business> selectBusinessList() {
        List<Business> list = nodeMapper.selectBusinessList();
        return list;
    }

    @Override
    public boolean insertNode(Node node) {
        //名字是key 不能一样
        if (nodeMapper.selectNodeName(node.getName())!= null){
            return false;

        }
        node.setCreateTime(LocalDateTime.now());
        node.setUpdateTime(LocalDateTime.now());
        node.setCreateUserId(1);
        nodeMapper.insertNode(node);
        return true;
    }

    /**
     * 待优化
     * @param nodeId
     * @return
     */
    @Override
    public List<VendingMachine> selectBigList(String nodeId) {
        //分三步，1 查VendingMachine 集合 遍历集合 分别 查 node region
        List<VendingMachine> vendingMachineList = nodeMapper.selectVendingMachine(nodeId);
        for (VendingMachine big : vendingMachineList) {
        //添加node
            //查node 查 type 查 region
           Node node = nodeMapper.selectNodeByNodeID(nodeId);
            Region region = nodeMapper.selectRegionByRegionId(node.getRegionId());
            Business business = nodeMapper.selectBusinessById(node.getBusinessId());
            Type type = nodeMapper.selectVMTypeByvmType(big.getVmType());
            //(小)加region
            node.setRegion(region);
            //(小)加business
            node.setBusinessType(business);
            //(大)加type 加node 加region
            big.setNode(node);
            big.setType(type);
            big.setRegion(region);
        }
        return vendingMachineList;
    }

    @Override
    public Boolean updateNode(String nodeId, Node node) {
        node.setUpdateTime(LocalDateTime.now());
        node.setId(Integer.valueOf(nodeId));
        log.info("String的返回值是：{}",nodeMapper.selectNodeName(node.getName()));
        if (nodeMapper.selectNodeName(node.getName())!= null){
            return false ;
        }
        nodeMapper.updateNode(node);
        return true;
    }

    @Override
    public Boolean deleteNode(String nodeId) {
        if (nodeMapper.selectTborderAndCollect(Integer.parseInt(nodeId))>0){
            return false;
        }
        nodeMapper.deleteNode(Integer.parseInt(nodeId));
        return true;
    }
}
